package com.per.note.ui.main;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.per.note.R;
import com.per.note.bean.MsgDay;
import com.per.note.db.ResultMessage;
import com.per.note.db.SqliteManage;
import com.per.note.db.url;
import com.per.note.utils.SDUtils;
import com.per.note.utils.HttpUtils;
import com.per.note.utils.JsonTools;
import com.per.note.ui.floatball.FloatWindowService;

import java.io.File;

public class MoreFragment extends Fragment {
    private TextView  mTv_clear, mTv_tellme, mTv_db;
	private ToggleButton mTogBtn;
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_more, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
		mTogBtn = (ToggleButton) view.findViewById(R.id.mTogBtn) ;
        mTv_clear = (TextView) view.findViewById(R.id.fragment_more_tv_clear);
        mTv_tellme = (TextView) view.findViewById(R.id.fragment_more_tv_tellme);
        mTv_db = (TextView) view.findViewById(R.id.fragment_more_tv_db);
        mTv_clear.setOnClickListener(mListener);
        mTv_tellme.setOnClickListener(mListener);
        mTv_db.setOnClickListener(mListener);
		mTogBtn.setOnCheckedChangeListener(cListener);
    }

    private View.OnClickListener mListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == null) return;
            switch (v.getId()) {
                case R.id.fragment_more_tv_clear:
                    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                        String path = Environment.getExternalStorageDirectory().getAbsolutePath()
                                + File.separator + "appX.DownloadApp.hiyo";
                        File file = new File(path);
                        file.mkdirs();
                        deleFile(file);
                        Toast.makeText(getActivity(), "清除完成", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.fragment_more_tv_db:
                    initDB();
                    break;
                case R.id.fragment_more_tv_tellme:
                    initData();
                    break;
                default:
                    break;
            }
        }
    };

    private void deleFile(File file) {
        if (file.isDirectory()) {
            File f[] = file.listFiles();
            for (int i = 0; i < f.length; i++) {
                if (f[i].isDirectory()) {
                    deleFile(f[i]);
                } else {
                    f[i].delete();
                }
            }
        }
        file.delete();
    }

	CompoundButton.OnCheckedChangeListener cListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            Intent intent = new Intent(getActivity(), FloatWindowService.class);
            if(isChecked){
                //打开悬浮球
                getActivity().startService(intent);
                //getActivity().finish();
            }
            else {
                //关闭悬浮球
                getActivity().stopService(intent);
            }
        }
    };


    private void initDB() {
        url url = new url();
        String path = url.getUrl()+"/AccountServer/uploadAction";
        //String JsonString = null;
        SqliteManage.QueryResult result = SqliteManage.getInstance(getActivity()).query("inout", null, null);
        if (result.cursor == null) return;
        while (result.cursor.moveToNext()) {
            MsgDay msgDay = new MsgDay(result.cursor);
            String path0 = path+msgDay.toString2();
            new MyTask().execute(path0);
            //ResultMessage result = JsonTools.getString("ResultMessage", JsonString);
            //if(result.getResultCode()==1){
            //    Toast.makeText(basicdata.this, result.getResultMessage(), Toast.LENGTH_SHORT).show();
            //}
        }
    }

    class MyTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            String jsonString = HttpUtils.getJsonContent(arg0[0]);
//			Person person = JsonTools.getPerson("person", jsonString);
            return jsonString;
        }
        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            //JsonString = result;
        }
    }
	
    private void initData() {
        SqliteManage.QueryResult result = SqliteManage.getInstance(getActivity()).query("inout", null, null);
        if (result.cursor == null) return;
        SDUtils.delete("天天记.csv");
        SDUtils.save("天天记.csv", "年," + "月," + "日," + "星期," + "收支," + "记录时间," + "金额," + "分类," + "账户," + "备注");
        while (result.cursor.moveToNext()) {
            MsgDay msgDay = new MsgDay(result.cursor);
            SDUtils.save("天天记.csv", msgDay.toString());
        }
        Toast.makeText(getActivity(), "成功保存在天天记文件夹", Toast.LENGTH_SHORT).show();
    }

}